package iPod;

import java.io.IOException;
import java.io.RandomAccessFile;

public class DataSet extends IDBChunk
{
	// valid data set types
	protected static final int TRACKLIST = 1;
	protected static final int PLAYLIST = 2;
	protected static final int PODCASTLIST = 3;
	protected static final int ALBUMLIST = 4;
	protected static final int SMARTPLAYLIST = 5;
	
	// Extra data in the header
	protected int dataSetType = 0;
	protected DataList datalist;
	
	public DataSet()
	{
		expectedID = "mhsd";
	}
	
	public void readChild(IDBChunk chunk, RandomAccessFile in) throws Exception
    {
		if (dataSetType == TRACKLIST && chunk.typeID.equalsIgnoreCase("mhlt"))
		{
			datalist = new TrackList();
			datalist.load(in);
		}
		else if (dataSetType == PLAYLIST && chunk.typeID.equalsIgnoreCase("mhlp"))
		{
			System.out.println("Skipping playlist list");
			datalist = new DataList();
			datalist.loadRecord(in);
			datalist.skip(in);
		}
		else if (dataSetType == PODCASTLIST && chunk.typeID.equalsIgnoreCase("mhlp"))
		{
			System.out.println("Skipping podcast list");
			datalist = new DataList();
			datalist.loadRecord(in);
			datalist.skip(in);
		}
		else if (dataSetType == ALBUMLIST && chunk.typeID.equalsIgnoreCase("mhla"))
		{
			System.out.println("Skipping album list");
			datalist = new DataList();
			datalist.loadRecord(in);
			datalist.skip(in);
		}
		else if (dataSetType == SMARTPLAYLIST && chunk.typeID.equalsIgnoreCase("mhlp"))
		{
			System.out.println("Skipping smart playlist list");
			datalist = new DataList();
			datalist.loadRecord(in);
			datalist.skip(in);
		}
		else 
		{
			super.readChild(chunk, in);
		}
    }
	
	public void loadHeader(RandomAccessFile in) throws Exception
    { 
		super.loadHeader(in);
		
		// Load extra dataset info
		in.read(item);
		dataSetType = DataUtils.littleEndianToInt(item);		
    }

	public int getDataSetType() {
		return dataSetType;
	}

	public DataList getDatalist() {
		return datalist;
	}	
}
